﻿/*
Post-Deployment Script Template
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.
 Use SQLCMD syntax to include a file in the post-deployment script.
 Example:      :r .\myfile.sql
 Use SQLCMD syntax to reference a variable in the post-deployment script.
 Example:      :setvar TableName MyTable
               SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/

BEGIN TRY
    BEGIN TRANSACTION

    DECLARE @dt_now   DATETIME,
            @nvc_user NVARCHAR(1024)
    SET @dt_now   = GETUTCDATE()
    SET @nvc_user = N'System'

    -- easyweb

    DECLARE @uid_easyweb_id UNIQUEIDENTIFIER,
            @vc_name        VARCHAR(512),
            @vc_version     VARCHAR(1024),
            @b_upgrade      BIT

    SET @uid_easyweb_id = 'F06D0FFB-670B-481A-9D24-6E6B4F3740BC'
    SET @vc_name        = 'EasyWeb'
    SET @vc_version     = '1.0.0.0'

    EXEC ew_sp_install_pre
        @uid_easyweb_id = @uid_easyweb_id,
        @vc_name        = @vc_name,
        @vc_version     = @vc_version,
        @b_upgrade      = @b_upgrade OUTPUT

    -- Do not install or upgrade
    IF @b_upgrade IS NULL OR @b_upgrade = 0
    BEGIN
        GOTO NON_INSTALL_END
    END


    -- cultures

    DECLARE @uid_culture_id UNIQUEIDENTIFIER

    SET @uid_culture_id = CONVERT(UNIQUEIDENTIFIER, '61d22c63-ba85-4e8d-b230-67545e6b88fe')
    EXEC ew_sp_install_cultures
        @uid_culture_id = @uid_culture_id, 
        @i_lcid         = 9, 
        @vc_name        = 'en', 
        @b_enabled      = 0, 
        @vc_description = 'English', 
        @dt_datetime    = @dt_now, 
        @nvc_user       = @nvc_user

    SET @uid_culture_id = CONVERT(UNIQUEIDENTIFIER, '97e46569-c16d-438c-a21b-d9c67d90d12b')
    EXEC ew_sp_install_cultures
        @uid_culture_id = @uid_culture_id, 
        @i_lcid         = 4,
        @vc_name        = 'zh-Hans',
        @b_enabled      = 0,
        @vc_description = 'Chinese (Simplified)', 
        @dt_datetime    = @dt_now, 
        @nvc_user       = @nvc_user


    -- configurations

    DECLARE @uid_configuration_id UNIQUEIDENTIFIER

    SET @uid_configuration_id = CONVERT(UNIQUEIDENTIFIER, '24664447-7293-45D5-A800-A3FE3B0ED822')
    EXEC ew_sp_install_configurations
        @uid_configuration_id = @uid_configuration_id,
        @vc_name              = 'system_prefix',
        @xml_value_1          = '<value></value>',
        @dt_datetime          = @dt_now,
        @nvc_user             = @nvc_user

    SET @uid_configuration_id = CONVERT(UNIQUEIDENTIFIER, 'EF90A8FC-44D1-4C59-B560-3F309C6F90C8')
    EXEC ew_sp_install_configurations
        @uid_configuration_id = @uid_configuration_id,
        @vc_name              = 'default_language',
        @xml_value_1          = '<value><value>en</value><client>False</client><default>True</default></value>',
        @dt_datetime          = @dt_now,
        @nvc_user             = @nvc_user

    -- Upgrade system version

    EXEC ew_sp_install_post
        @uid_easyweb_id = @uid_easyweb_id,
        @vc_name        = @vc_name,
        @vc_version     = @vc_version,
        @dt_datetime    = @dt_now,
        @nvc_user       = @nvc_user

NON_INSTALL_END:
    COMMIT
END TRY
BEGIN CATCH
    DECLARE @i_error    INT,
            @vc_message VARCHAR(4000)
    SELECT @i_error = ERROR_NUMBER(), @vc_message = ERROR_MESSAGE()
    ROLLBACK
    RAISERROR ('Post deployment SQL error: %d: %s', 16, 1, @i_error, @vc_message)
END CATCH
